//
// TypeReference.cs
//
// Author:
//   Jb Evain (jbevain@gmail.com)
//
// (C) 2005 - 2008  Jb Evain
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;

using Mono.Cecil.Metadata;

namespace Mono.Cecil {

	public class TypeReference : MemberReference, IGenericParameterProvider, IGenericContext {

		string @namespace;
		bool value_type;
		IMetadataScope scope;
		ModuleDefinition module;

		internal ElementType etype = ElementType.None;

		protected GenericParameterCollection generic_parameters;

		public virtual string Namespace {
			get { return @namespace; }
			set { @namespace = value; }
		}

		public virtual bool IsValueType {
			get { return value_type; }
			set { value_type = value; }
		}

		public virtual ModuleDefinition Module {
			get { return module; }
			internal set { module = value; }
		}

		IGenericParameterProvider IGenericContext.Type {
			get { return this; }
		}

		IGenericParameterProvider IGenericContext.Method {
			get { return null; }
		}

		GenericParameterType IGenericParameterProvider.GenericParameterType {
			get { return GenericParameterType.Type; }
		}

		public virtual bool HasGenericParameters {
			get {
				return generic_parameters != null && generic_parameters.Count > 0;
			}
		}

		public virtual GenericParameterCollection GenericParameters {
			get {
				if (generic_parameters != null)
					return generic_parameters;

				return generic_parameters = new GenericParameterCollection ();
			}
		}

		public virtual IMetadataScope Scope {
			get {
				if (this.DeclaringType != null)
					return this.DeclaringType.Scope;

				return scope;
			}
			internal set { scope = value; }
		}

		public bool IsNested {
			get { return this.DeclaringType != null; }
		}

		public override string FullName {
			get {
				if (IsNested)
					return DeclaringType.FullName + "/" + Name;

				if (string.IsNullOrEmpty (@namespace))
					return Name;

				return @namespace + "." + Name;
			}
		}

		public virtual bool IsByReference {
			get { return false; }
		}

		public virtual bool IsPointer {
			get { return false; }
		}

		public virtual bool IsSentinel {
			get { return false; }
		}

		public virtual bool IsArray {
			get { return false; }
		}

		public virtual bool IsGenericParameter {
			get { return false; }
		}

		public virtual bool IsGenericInstance {
			get { return false; }
		}

		public virtual bool IsRequiredModifier {
			get { return false; }
		}

		public virtual bool IsOptionalModifier {
			get { return false; }
		}

		public virtual bool IsPinned {
			get { return false; }
		}

		public virtual bool IsFunctionPointer {
			get { return false; }
		}

		public virtual bool IsDefinition {
			get { return false; }
		}

		protected TypeReference (string name, string ns) : base (name)
		{
			this.@namespace = ns;
		}

		internal TypeReference (string name, string ns, IMetadataScope scope) : this (name, ns)
		{
			this.scope = scope;
		}

		public TypeReference (string name, string ns, IMetadataScope scope, bool valueType) :
			this (name, ns, scope)
		{
			value_type = valueType;
		}

		public virtual TypeReference GetElementType ()
		{
			return this;
		}

		public virtual TypeDefinition Resolve ()
		{
			if (module == null)
				throw new NotSupportedException ();

			return module.Resolve (this);
		}

		public override string ToString ()
		{
			return this.FullName;
		}
	}
}
